package net.oschina.app.viewpagerfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import net.oschina.app.R;
import net.oschina.app.adapter.NewsPagerAdapter;
import net.oschina.app.viewpagerfragment.newsdetailfragment.BlogFragment;
import net.oschina.app.viewpagerfragment.newsdetailfragment.HotFragment;
import net.oschina.app.viewpagerfragment.newsdetailfragment.NewsFragment;
import net.oschina.app.viewpagerfragment.newsdetailfragment.RecommonFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by foty on 2017/4/1.
 * ?
 */

public class NewFragment extends Fragment {
    @BindView(R.id.tl_newsfrgm_guide)
    TabLayout mTlNewsfrgmGuide;
    @BindView(R.id.vp_newsfrgm_container)
    ViewPager mVpNewsfrgmContainer;
    @BindView(R.id.ll_newsfrgm)
    LinearLayout mLlNewsfrgm;
    private List<Fragment> mFragmentList;
    private List<String> mTitleList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //找到综合界面的容器
        View view = inflater.inflate(R.layout.view_newsfragment, container, false);
        ButterKnife.bind(this, view);

        //创建TabLayout标签对应的ViewPager页面
        NewsFragment nf = new NewsFragment();
        HotFragment hf = new HotFragment();
        BlogFragment bf = new BlogFragment();
        RecommonFragment rf = new RecommonFragment();

        //将ViewPager页面添加进集合
        mFragmentList = new ArrayList<>();
        mFragmentList.add(nf);
        mFragmentList.add(hf);
        mFragmentList.add(bf);
        mFragmentList.add(rf);

        //将TabLayout标签名添加进集合
        mTitleList = new ArrayList<>();
        mTitleList.add("资讯");
        mTitleList.add("热点");
        mTitleList.add("博客");
        mTitleList.add("推荐");

        //设置TabLayout的标签模式
        mTlNewsfrgmGuide.setTabMode(TabLayout.MODE_FIXED);

        //给TabLayout添加标签
        for (int i = 0; i < mTitleList.size(); i++) {
            mTlNewsfrgmGuide.addTab(mTlNewsfrgmGuide.newTab().setText(mTitleList.get(i)));
        }

        //给ViewPager添加适配器
        mVpNewsfrgmContainer.setAdapter(new NewsPagerAdapter(getChildFragmentManager(), mFragmentList,
                mTitleList));

        //将TabLayout与ViewPager关联
        mTlNewsfrgmGuide.setupWithViewPager(mVpNewsfrgmContainer);
        return view;
    }
}
